<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kota_user_model extends CI_Model{
	function __construct() {
		parent::__construct();
	}
	
	function get_all(){
                $this->db->select('*,TbMDKota.Nama AS nama_kota, TbUser.nama AS nama_user,TbUser.user_id AS user_id');
                $this->db->join('TblKotaUser','TblKotaUser.user_id=TbUser.user_id','left');
                $this->db->join('TbMDKota','Kode=TblKotaUser.kode_kota','left');
                $this->db->where('user_level','4');
		return $this->db->get('TbUser')->result_array();
	}
	
	function get_one($id){
                $this->db->select('*,TbMDKota.Nama AS nama_kota, TbUser.nama AS nama_user,TbUser.user_id AS user_id');
                $this->db->join('TblKotaUser','TblKotaUser.user_id=TbUser.user_id','left');
                $this->db->join('TbMDKota','Kode=TblKotaUser.kode_kota','left');
		$query = $this->db->get_where('TbUser',array('TbUser.user_id'=>$id));
		if($query->num_rows()>0){
			return $query->row_array();
		}else{
			return FALSE;
		}
	}
        
        function is_exist($id)
        {
            $query = $this->db->get_where('TblKotaUser', array(
			'user_id' => $id
				));
            if ($query->num_rows == 1) {
			return TRUE;
		} else {
			return FALSE;
		}
        }
	
	function insert_user($id,$kota){
            if (!$this->is_exist($id))
		$this->db->insert('TblKotaUser',array('user_id'=>$id,'kode_kota'=>$kota));
            else
            {
                $this->db->where('user_id',$id);
                $this->db->update('TblKotaUser',array('kode_kota'=>$kota));
            }
	}
	
}

/** End of file */